﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace JESAI.DistributedLock;

/// <summary>
/// 可以升级为写锁的 <see cref="IDistributedSynchronizationHandle"/>
/// </summary>
public interface IDistributedLockUpgradeableHandle : IDistributedSynchronizationHandle
{
    /// <summary>
    /// 尝试同步升级WRITE锁。与其他WRITE锁或升级锁不兼容
    /// </summary>
    bool TryUpgradeToWriteLock(TimeSpan timeout = default, CancellationToken cancellationToken = default);

    /// <summary>
    /// 同步升级到WRITE锁。与其他WRITE锁或升级锁不兼容
    /// </summary>
    void UpgradeToWriteLock(TimeSpan? timeout = null, CancellationToken cancellationToken = default);

    /// <summary>
    /// 尝试异步升级WRITE锁。与其他WRITE锁或升级锁不兼容
    /// </summary>
    ValueTask<bool> TryUpgradeToWriteLockAsync(TimeSpan timeout = default, CancellationToken cancellationToken = default);

    /// <summary>
    /// 异步升级到WRITE锁。与其他WRITE锁或升级锁不兼容
    /// </summary>
    ValueTask UpgradeToWriteLockAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default);
}
